package com.csu.petstore.controller;

import com.alibaba.fastjson2.JSONArray;
import com.csu.petstore.common.CommonResponse;
import com.csu.petstore.log.Syslog;
import com.csu.petstore.pojo.Cart;
import com.csu.petstore.pojo.Item;
import com.csu.petstore.service.CartService;
import com.csu.petstore.service.CatalogService;
import com.csu.petstore.vo.CategoryVo;
import com.csu.petstore.vo.ItemVo;
import com.csu.petstore.vo.ProductVo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Validated
public class CatalogController {
    @Autowired
    private CatalogService catalogService;
    @Autowired
    private CartService cartService;
//    //跳转主页面
//   @GetMapping("/index")
//   @Syslog(modelName = "主界面",methodDesc = "进入主界面",methodType = 2)
//    public String index(){
//
//       return"catalog/main";
//   }
    //Category页面
    @GetMapping("/Categories/{categoryId}")
    @Syslog(modelName = "种类",methodDesc = "进入种类界面",methodType = 2)
    public CommonResponse<CategoryVo> viewCategory(@PathVariable("categoryId")String categoryId) {
        return catalogService.getCategory(categoryId);
    }
    //Product页面
    @GetMapping("/Products/{productId}")
    @Syslog(modelName = "商品类",methodDesc = "进入商品类界面",methodType = 2)
    public CommonResponse<ProductVo> viewProduct(@PathVariable("productId")String productId) {
        return catalogService.getProduct(productId);
    }
    //Item页面
    @GetMapping("/items/{itemId}")
    @Syslog(modelName = "具体商品",methodDesc = "进入具体商品界面",methodType = 2)
    public CommonResponse<ItemVo> getItem(@PathVariable("itemId") String  itemId) {
        System.out.println(catalogService.getItem(itemId));
        return catalogService.getItem(itemId);
    }
    //搜索结果页面
    @GetMapping("/search/{keyword}")
    @Syslog(modelName = "搜索",methodDesc = "搜索信息",methodType = 2)
    public CommonResponse<CategoryVo> viewSearchProduct(@PathVariable("keyword") String keyword) {
        return catalogService.getSearchProduct(keyword);
    }
    //搜索栏自动补全
//    @GetMapping("searchProduct")
//    @ResponseBody
//    public Map<String,Object> searchProduct(String keyword) {
//       Map<String,Object> map =new HashMap<>();
//        CategoryVo categoryVo =catalogService.getSearchProduct(keyword);
//        map.put("products",categoryVo.getProductList());
//        return map;
//    }

    @GetMapping("/addItem")
    @Syslog(modelName = "购物车",methodDesc = "添加商品进入购物车",methodType = 2)
    public String addItem(@RequestParam("itemid")String itemId, HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        Cart cart = cartService.addItemToCart(itemId , username);

        session.setAttribute("cart" , cart);

        return "/cart/cart";
    }

}